package com.lrm.biz.impl;

import com.lrm.biz.IBlogBiz;
import com.lrm.dao.BlogJPA;
import com.lrm.exception.MyException;
import com.lrm.pojo.Blog;
import com.lrm.pojo.Type;
import com.lrm.util.MarkdownUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import javax.transaction.Transactional;
import java.util.*;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2017/12/1.
 * @Modified By:
 */
@Service
@Transactional
public class BlogBizImpl implements IBlogBiz {

    @Autowired
    private BlogJPA blogJPA;


    @Override
    public Blog getOne(Long id) {
        Blog one = blogJPA.getOne(id);
        if (one == null) {
            throw new MyException("该博客不存在", 2);
        }
        Blog blog = new Blog();
        BeanUtils.copyProperties(one, blog);
        return blog;
    }

    @Override
    public Page<Blog> listBlog(Pageable pageable, String title, Long typeid, boolean recommend) {
        Page<Blog> all = blogJPA.findAll(new Specification<Blog>() {
            /**
             * jap复杂查询
             * @param root
             * @param cq
             * @param cb
             * @return
             */
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> cq,
                                         CriteriaBuilder cb) {
                ArrayList<Predicate> predicates = new ArrayList<>();
                if (!"".equals(title) && title != null) {
                    predicates.add(cb.like(root.<String>get("title"), "%" + title + "%"));
                }
                if (typeid != null) {
                    predicates.add(cb.equal(root.<Type>get("type").get("id"), typeid));
                }
                if (recommend) {

                    predicates.add(cb.equal(root.<Boolean>get("recommend"), recommend));
                }
                cq.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        }, pageable);
        return all;
    }

    @Override
    public Page<Blog> listBlog(Pageable pageable) {
        Page<Blog> all = blogJPA.findAll(pageable);
        return all;
    }

    @Override
    public Page<Blog> listBlog(Pageable pageable, Long tagId) {
        return blogJPA.findAll(new Specification<Blog>() {
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> cq,
                                         CriteriaBuilder cb) {
                //关联查询
                Join join = root.join("tags");
                return cb.equal(join.get("id"), tagId);
            }
        }, pageable);

    }

    @Override
    public Page<Blog> blogSenrchList(String senrch, Pageable pageable) {
        Page<Blog> blogs = blogJPA.blogSenrchList("%" + senrch + "%", pageable);
        return blogs;
    }

    @Override
    public List<Blog> blogrecommendList(Pageable pageable) {
        List<Blog> blogs = blogJPA.blogrecommendList(pageable);
        return blogs;
    }

    @Override
    public Map<String, List<Blog>> archiveBlog() {
        Map<String, List<Blog>> map=new HashMap<>();
        List<String> years=blogJPA.findGroupYear();
        for (String year : years) {
            List<Blog> blogList = blogJPA.findByYear(year);
            map.put(year,blogList);
        }
        return map;
    }

    @Override
    public Blog saveBlog(Blog blog) {
        blog.setCreateTime(new Date());
        blog.setUpdataTime(new Date());
        return blogJPA.save(blog);
    }

    @Override
    public Blog updataBlog(Long id, Blog blog) {
        Blog one = blogJPA.getOne(id);
        if (one == null) {
            throw new MyException("该博客不存在", 2);
        }
        BeanUtils.copyProperties(one, blog);
        Blog save = blogJPA.save(one);
        return save;
    }

    @Override
    public Boolean delBlog(Long id) {
        try {
            blogJPA.delete(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Blog findByIdmk(Long id) {
        blogJPA.updataViews(id);
        Blog one = blogJPA.getOne(id);
        if (one == null) {
            throw new MyException("博客资源不存在", 2);
        }
        String content = one.getContent();
        String s = MarkdownUtils.markdownToHtmlExtensions(content);
        Blog blog = new Blog();
        BeanUtils.copyProperties(one, blog);
        blog.setContent(s);
        return blog;
    }


}
